<?php
/**
 * $Id: submit.php v 1.0.4 21 June 2005 John N Exp $
 * Module: WF-Links
 * Version: v1.0.3
 * Release Date: 21 June 2005
 * Developer: John N
 * Team: WF-Projects
 * Licence: GNU
 */

include 'header.php';
include XOOPS_ROOT_PATH . '/header.php';
include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';

$mytree = new XoopsTree( $xoopsDB -> prefix( WFLINKS_CAT ), 'cid', 'pid' );
global $wfmyts, $xoopsModuleConfig;

$cid = wfl_cleanRequestVars( $_REQUEST, 'cid', 0 );
$lid = wfl_cleanRequestVars( $_REQUEST, 'lid', 0 );

if ( false == checkgroups( $cid, 'WFLinkSubPerm' ) )
{
    redirect_header( "index.php", 1, _MD_WFL_NOPERMISSIONTOPOST );
    exit();
} 

if ( true == checkgroups( $cid, 'WFLinkSubPerm' ) )
{
    if ( wfl_cleanRequestVars( $_REQUEST, 'submit', 0 ) )
    {
        if ( false == checkgroups( $cid, 'WFLinkSubPerm' ) )
        {
            redirect_header( "index.php", 1, _MD_WFL_NOPERMISSIONTOPOST );
            exit();
        } 

        $submitter = ( is_object( $xoopsUser ) && !empty( $xoopsUser ) ) ? $xoopsUser -> getVar( 'uid' ) : 0;
        $forumid = wfl_cleanRequestVars( $_REQUEST, 'forumid', 0 );
        $offline = wfl_cleanRequestVars( $_REQUEST, 'offline', 0 );
        $urlrating = wfl_cleanRequestVars( $_REQUEST, 'urlrating', 6 );
        $notifypub = wfl_cleanRequestVars( $_REQUEST, 'notifypub', 0 );
        $approve = wfl_cleanRequestVars( $_REQUEST, 'approve', 0 );
        $url = $wfmyts -> addslashes( ltrim($_POST["url"]) );
        $title = $wfmyts -> addslashes( ltrim( $_REQUEST["title"] ) );
        $description = $wfmyts -> addslashes( ltrim( $_REQUEST["description"] ) );
        $date = time();
        $publishdate = 0;
        $ipaddress = $_SERVER['REMOTE_ADDR'];

        if ( $lid == 0 )
        {
            $status = 0;
            $publishdate = 0;
            $message = _MD_WFL_THANKSFORINFO;
            if ( true == checkgroups( $cid, 'WFLinkAutoApp' ) )
            {
                $publishdate = time();
                $status = 1;
                $message = _MD_WFL_ISAPPROVED;
            } 
            $sql = "INSERT INTO " . $xoopsDB -> prefix( WFLINKS_LINKS ) . "	(lid, cid, title, url, submitter, status, date, hits, rating, votes, comments, forumid, published, expired, offline, description, ipaddress, notifypub, urlrating) ";
            $sql .= " VALUES 	('', $cid, '$title', '$url', '$submitter', '$status', '$date', 0, 0, 0, 0, '$forumid', '$publishdate', 0, '$offline', '$description', '$ipaddress', '$notifypub', '$urlrating')";
            if ( !$result = $xoopsDB -> query( $sql ) )
            {
                $_error = $xoopsDB -> error() . " : " . $xoopsDB -> errno();
                XoopsErrorHandler_HandleError( E_USER_WARNING, $_error, __FILE__, __LINE__ );
            } 
            $newid = $xoopsDB -> getInsertId();

            /**
             * Notify of new link (anywhere) and new link in category
             */
            $notification_handler = &xoops_gethandler( 'notification' );

            $tags = array();
            $tags['LINK_NAME'] = $title;
            $tags['LINK_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/singlelink.php?cid=' . $cid . '&amp;lid=' . $newid;
            
			$sql = "SELECT title FROM " . $xoopsDB -> prefix( 'mylinks_cat' ) . " WHERE cid=" . $cid;
            $result = $xoopsDB -> query( $sql );
            $row = $xoopsDB -> fetchArray( $result );

            $tags['CATEGORY_NAME'] = $row['title'];
            $tags['CATEGORY_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/viewcat.php?cid=' . $cid;
            if ( true == checkgroups( $cid, 'WFLinkAutoApp' ) )
            {
                $notification_handler -> triggerEvent( 'global', 0, 'new_link', $tags );
                $notification_handler -> triggerEvent( 'category', $cid, 'new_link', $tags );
                redirect_header( 'index.php', 2, _MD_WFL_ISAPPROVED );
            } 
            else
            {
                $tags['WAITINGFILES_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/admin/newlinks.php';
                $notification_handler -> triggerEvent( 'global', 0, 'link_submit', $tags );
                $notification_handler -> triggerEvent( 'category', $cid, 'link_submit', $tags );
                if ( $notifypub )
                {
                    include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
                    $notification_handler -> subscribe( 'link', $newid, 'approve', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE );
                } 
                redirect_header( 'index.php', 2, _MD_WFL_THANKSFORINFO );
            } 
        } 
        else
        {
            if ( true == checkgroups( $cid, 'WFLinkAutoApp' ) || $approve == 1 )
            {
                $updated = time();
                $sql = "UPDATE " . $xoopsDB -> prefix( WFLINKS_LINKS ) . " SET cid=$cid, title='$title', url='$url', updated='$updated', offline='$offline', description='$description', ipaddress='$ipaddress', notifypub='$notifypub', urlrating='$urlrating' WHERE lid =" . $lid;
                if ( !$result = $xoopsDB -> query( $sql ) )
                {
                    $_error = $xoopsDB -> error() . " : " . $xoopsDB -> errno();
                    XoopsErrorHandler_HandleError( E_USER_WARNING, $_error, __FILE__, __LINE__ );
                } 

                $notification_handler = &xoops_gethandler( 'notification' );
                $tags = array();
                $tags['LINK_NAME'] = $title;
                $tags['LINK_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/singlelink.php?cid=' . $cid . '&amp;lid=' . $lid;
                $sql = "SELECT title FROM " . $xoopsDB -> prefix( WFLINKS_CAT ) . " WHERE cid=" . $cid;
                $result = $xoopsDB -> query( $sql );
                $row = $xoopsDB -> fetchArray( $result );
                $tags['CATEGORY_NAME'] = $row['title'];
                $tags['CATEGORY_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/viewcat.php?cid=' . $cid;

                $notification_handler -> triggerEvent( 'global', 0, 'new_link', $tags );
                $notification_handler -> triggerEvent( 'category', $cid, 'new_link', $tags );
                $_message = _MD_WFL_ISAPPROVED;
            } 
            else
            {
                $modifysubmitter = $xoopsUser -> uid();
                $requestid = $modifysubmitter;
                $requestdate = time();
                $updated = wfl_cleanRequestVars( $_REQUEST, 'up_dated', time() );
                $sql = "INSERT INTO " . $xoopsDB -> prefix( WFLINKS_MOD ) . " (requestid, lid, cid, title, url, forumid, description, modifysubmitter, requestdate)";
                $sql .= " VALUES ('', $lid, $cid, '$title', '$url', '$forumid', '$description', '$urlrating' '$modifysubmitter', '$requestdate')";
                if ( !$result = $xoopsDB -> query( $sql ) )
                {
                    $_error = $xoopsDB -> error() . " : " . $xoopsDB -> errno();
                    XoopsErrorHandler_HandleError( E_USER_WARNING, $_error, __FILE__, __LINE__ );
                } 

                $tags = array();
                $tags['MODIFYREPORTS_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/admin/index.php?op=listModReq';
                $notification_handler = &xoops_gethandler( 'notification' );
                $notification_handler -> triggerEvent( 'global', 0, 'link_modify', $tags );

                $tags['WAITINGFILES_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/admin/index.php?op=listNewlinks';
                $notification_handler -> triggerEvent( 'global', 0, 'link_submit', $tags );
                $notification_handler -> triggerEvent( 'category', $cid, 'link_submit', $tags );
                if ( $notifypub )
                {
                    include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
                    $notification_handler -> subscribe( 'link', $newid, 'approve', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE );
                } 
                $_message = _MD_WFL_THANKSFORINFO;
            } 
            redirect_header( "index.php", 2, $_message );
        } 
    } 
    else
    {
        global $xoopsModuleConfig;

        $approve = wfl_cleanRequestVars( $_REQUEST, 'approve', 0 );
        /*
		* Show disclaimer
		*/
        if ( $xoopsModuleConfig['showdisclaimer'] && !isset( $_GET['agree'] ) && $approve == 0 )
        {
            echo "<br /><div style='text-align: center;'>" . wfl_imageheader() . "</div><br />\n";
            echo "<h4>" . _MD_WFL_DISCLAIMERAGREEMENT . "</h4>\n";
            echo "<div>" . $wfmyts -> displayTarea( $xoopsModuleConfig['disclaimer'], 1, 1, 1, 1, 1 ) . "</div>\n";
            echo "<form action='submit.php' method='post'>\n";
            echo "<div style='text-align: center;'>" . _MD_WFL_DOYOUAGREE . "</b><br /><br />\n";
            echo "<input type='button' onclick='location=\"submit.php?agree=1\"' class='formButton' value='" . _MD_WFL_AGREE . "' alt='" . _MD_WFL_AGREE . "' />\n";
            echo "&nbsp;\n";
            echo "<input type='button' onclick='location=\"index.php\"' class='formButton' value='" . _CANCEL . "' alt='" . _CANCEL . "' />\n";
            echo "</div></form>\n";
            include XOOPS_ROOT_PATH . '/footer.php';
            exit();
        } 
        echo "<br /><div style='text-align: center;'>" . wfl_imageheader() . "</div><br />\n";
        echo "<div>" . _MD_WFL_SUB_SNEWMNAMEDESC . "</div>\n<br />\n";

        $sql = 'SELECT * FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . ' WHERE lid=' . intval( $lid );
        $link_array = $xoopsDB -> fetchArray( $xoopsDB -> query( $sql ) );

        $lid = $link_array['lid'] ? $link_array['lid'] : 0;
        $cid = $link_array['cid'] ? $link_array['cid'] : 0;
        $title = $link_array['title'] ? $wfmyts -> htmlSpecialCharsStrip( $link_array['title'] ) : '';
        $url = $link_array['url'] ? $wfmyts -> htmlSpecialCharsStrip( $link_array['url'] ) : 'http://';
        $publisher = $link_array['publisher'] ? $wfmyts -> htmlSpecialCharsStrip( $link_array['publisher'] ) : '';
        $screenshot = $link_array['screenshot'] ? $wfmyts -> htmlSpecialCharsStrip( $link_array['screenshot'] ) : '';
        $description = $link_array['description'] ? $wfmyts -> htmlSpecialCharsStrip( $link_array['description'] ) : '';
        $published = $link_array['published'] ? $link_array['published'] : 0;
        $expired = $link_array['expired'] ? $link_array['expired'] : 0;
        $updated = $link_array['updated'] ? $link_array['updated'] : 0;
        $offline = $link_array['offline'] ? $link_array['offline'] : 0;
        $forumid = $link_array['forumid'] ? $link_array['forumid'] : 0;
        $ipaddress = $link_array['ipaddress'] ? $link_array['ipaddress'] : 0;
        $notifypub = $link_array['notifypub'] ? $link_array['notifypub'] : 0;
        $urlrating = $link_array['urlrating'] ? $link_array['urlrating'] : 1;

        $sform = new XoopsThemeForm( _MD_WFL_SUBMITCATHEAD, "storyform", xoops_getenv( 'PHP_SELF' ) );
        $sform -> setExtra( 'enctype="multipart/form-data"' );
        $sform -> addElement( new XoopsFormText( _MD_WFL_FILETITLE, 'title', 50, 255, $title ), true );
        $sform -> addElement( new XoopsFormText( _MD_WFL_DLURL, 'url', 50, 255, $url ), false );
        $mytree = new XoopsTree( $xoopsDB -> prefix( WFLINKS_CAT ), 'cid', 'pid' );

        $submitcats = array();
        $sql = 'SELECT * FROM ' . $xoopsDB -> prefix( WFLINKS_CAT ) . ' ORDER BY title';
        $result = $xoopsDB -> query( $sql );
        while ( $myrow = $xoopsDB -> fetchArray( $result ) )
        {
            if ( true == checkgroups( $myrow['cid'], 'WFLinkSubPerm' ) )
            {
                $submitcats[$myrow['cid']] = $myrow['title'];
            } 
        } 
        $cat_select = new XoopsFormSelect( _MD_WFL_CATEGORYC, 'cid', $cid );
        $cat_select -> addOptionArray( $submitcats );
        $sform -> addElement( $cat_select );

        $urlrating_array = wfl_rating_system();
        $urlrating_select = new XoopsFormSelect( _MD_WFL_LINK_DIRCA, 'urlrating', $urlrating );
        $urlrating_select -> addOptionArray( $urlrating_array );
        $sform -> addElement( $urlrating_select );
        $sform -> addElement( new XoopsFormDhtmlTextArea( _MD_WFL_DESCRIPTIONC, 'description', $description, 15, 50 ), true );
        $option_tray = new XoopsFormElementTray( _MD_WFL_OPTIONS, '<br />' );

        if ( !$approve )
        {
            $notify_checkbox = new XoopsFormCheckBox( '', 'notifypub' );
            $notify_checkbox -> addOption( 1, _MD_WFL_NOTIFYAPPROVE );
            $option_tray -> addElement( $notify_checkbox );
        } 
        else
        {
            $sform -> addElement( new XoopsFormHidden( 'notifypub', 0 ) );
        } 

        if ( true == checkgroups( $cid, 'WFLinkAppPerm' ) && $lid > 0 )
        {
            $approve_checkbox = new XoopsFormCheckBox( '', 'approve', $approve );
            $approve_checkbox -> addOption( 1, _MD_WFL_APPROVE );
            $option_tray -> addElement( $approve_checkbox );
        } 
        else if ( true == checkgroups( $cid, 'WFLinkAutoApp' ) )
        {
            $sform -> addElement( new XoopsFormHidden( 'approve', 1 ) );
        } 
        else
        {
            $sform -> addElement( new XoopsFormHidden( 'approve', 0 ) );
        } 
        $sform -> addElement( $option_tray );
        $button_tray = new XoopsFormElementTray( '', '' );
        $button_tray -> addElement( new XoopsFormButton( '', 'submit', _SUBMIT, 'submit' ) );
        $button_tray -> addElement( new XoopsFormHidden( 'lid', $lid ) );
        $sform -> addElement( $button_tray );
        $sform -> display();
        include XOOPS_ROOT_PATH . '/footer.php';
    } 
} 
else
{
    redirect_header( 'index.php', 2, _MD_WFL_NOPERMISSIONTOPOST );
    exit();
} 

?>